package demo1;

import java.util.Arrays;

public class MyArrayList implements IList {
    public int[] elem;
    public int usedSize;

    //提供构造方法来初始化
    public MyArrayList(){
        this.elem = new int[Constant.DEFAULT_CAPACITY];
    }
    @Override
    public void add(int data) {

        //判断顺序表是否满，是否需要进行扩容
        if(isFull()){
            grow();
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }

    public boolean isFull(){

        return this.usedSize ==this.elem.length;
    }
    //扩容
    private void grow(){

        this.elem = Arrays.copyOf(this.elem,2*elem.length);
    }
    @Override
    public void add(int pos, int data) {
        //判断数组是否是满的，需要扩容，前后没关系
        if(isFull()){
            grow();
        }
        //检查pos位置是否合法
        checkPosAdd(pos,Constant.ADD_POS_ILLEGALITY);
        //判断是否是放在usedSize（即最后一个）的位置(可有可无）
        if(pos==usedSize){
            elem[pos] = data;
            usedSize++;
            return;
        }
        //移动数据
        for(int i = usedSize-1;i <= pos ;i--){
            elem[i+1] = elem[i];
            usedSize++;
        }
        elem[pos] = data;
        usedSize++;
    }

    private void checkPosAdd(int pos,String msg){
        if(pos < 0||pos >usedSize){
            throw new PosIllegality(msg);
        }
    }
    @Override
    public boolean contains(int toFind) {
        for(int i = 0;i<usedSize;i++){
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i = 0;i < usedSize;i++){
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(isEmpty()){

            throw new EmptyException(Constant.EMPTY_LIST);
            
        }
        //判断pos位置是否合法
        checkPos(pos,Constant.GET_POS_ILLEGALITY+pos);

        return elem[pos];
    }

    private  void checkPos(int pos,String msg){
        if(pos<0||pos>=usedSize){
            throw new PosIllegality(msg);
        }
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }
    @Override
    public void set(int pos, int value) {
        if(isEmpty()){
            throw new EmptyException(Constant.EMPTY_LIST);
        }
        checkPos(pos,Constant.SET_POS_ILLEGALITY);
        return elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        if(isEmpty()){
            throw new EmptyException(Constant.EMPTY_LIST);
        }
        int index = indexOf(toRemove);
        if(index==-1){
            System.out.println(Constant.NO_EXIT);
        }
        for(int i = index;i<usedSize-1;i++){
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }

    @Override
    public void display() {
        for(int i = 0;i <this.usedSize;i++){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
}
